package com.xliee;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

public class CodeGenerator {

    public static void main(String[] args) {

        String sqlUrl = "jdbc:mysql://127.0.0.1:3306/xliee";
        String sqlUserName = "xmliss";
        String sqlPassword = "123456";

        String author = "lisongsong@xliee.com";
        String timePattern = "yyyy-MM-dd HH:mm:ss";

        String projectUrl = "C:/Users/wlissy/IdeaProjects/xliee";
        String parentPack = "com.xliee.modules";
        String moduleName = "vehicle";
        String dataTables = "yunxing_vehicle";


        FastAutoGenerator.create(sqlUrl, sqlUserName, sqlPassword)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .disableOpenDir()   //不打开输出目录
//                            .enableSwagger() // 开启 swagger 模式
                            .commentDate(timePattern)
                            .outputDir(projectUrl + "/src/main/java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent(parentPack)  // 设置父包名
                                .moduleName(moduleName)  // 设置父包模块名
                                .pathInfo(Collections.singletonMap(OutputFile.xml, projectUrl + "/src/main/resources/mapper/" + moduleName)) // 设置mapperXml生成路径
                )
                .strategyConfig(builder -> {
                    builder.addInclude(dataTables) // 设置需要生成的表名
                            .addTablePrefix("tb_", "c_", "ad_", "yunxing_");   // 设置过滤表前缀
                    builder.controllerBuilder()        //Controller格式
//                            .enableFileOverride()
                    ;
                    builder.serviceBuilder()            //Service格式
//                            .enableFileOverride()
                    ;
                    builder.mapperBuilder()             //mapper格式
//                            .enableFileOverride()
                    ;
                    builder.entityBuilder()             //设置entity格式
                            .enableLombok()             //开启lombok
                            .idType(IdType.AUTO)
                            .formatFileName("%sEntity")
                            .enableFileOverride()
                    ;
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
